<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;
use think\facade\View;
use think\helper\Str;
use think\exception\ValidateException;


// 概况数据
class Total extends BaseController
{
    public function index()
    {
        return View::fetch();
    }


    public function getCourseList()
    {
        $type = $this->request->get('type');

        //+热门课程--e-learning
        $month_first = strtotime(date("Y-m-01", time()));
        $month_end = strtotime(date("Y-m-t", time()))+86399;
        if (!isset($type)) {
            $data = Db::name('learning_user')
                ->where('add_time', 'between', [$month_first, $month_end])
                ->field('COUNT(userid) as count, learning_id')
                ->group('learning_id')
                ->limit(20)
                ->order('count', 'desc')
                ->select()
                ->toArray();

            foreach ($data as $k => $v) {
                $data[$k]['ranking'] = $k+1;
                $data[$k]['title'] = Db::name('learning')->where('id', '=', $v['learning_id'])->value('title');
            }
        } else {
            if ($type == 1) {
                $data = Db::name('learning_user')
                    ->where('add_time', 'between', [$month_first, $month_end])
                    ->field('COUNT(userid) as count, learning_id')
                    ->group('learning_id')
                    ->limit(20)
                    ->order('count', 'desc')
                    ->select()
                    ->toArray();

                foreach ($data as $k => $v) {
                    $data[$k]['ranking'] = $k+1;
                    $data[$k]['title'] = Db::name('learning')->where('id', '=', $v['learning_id'])->value('title');
                }
            } elseif ($type == 2) {
                //轻学堂
                $data = Db::name('qxt_user')
                    ->where('add_time', 'between', [$month_first, $month_end])
                    ->where('courses_id', '<>', 10001)
                    ->field('COUNT(uid) as count, courses_id')
                    ->group('courses_id')
                    ->limit(20)
                    ->order('count', 'desc')
                    ->select()
                    ->toArray();
                foreach ($data as $ke => $va) {
                    $data[$ke]['ranking'] = $ke+1;
                    $data[$ke]['title'] = Db::name('qxt_courses')->where('id', '=', $va['class_id'])->value('courseTitle');
                }
            } elseif ($type == 4) {
                //直播课
                $data = Db::name('live_user')
                    ->where('add_time', 'between', [$month_first, $month_end])
                    ->field('COUNT(userid) as count, live_id')
                    ->group('live_id')
                    ->limit(20)
                    ->order('count', 'desc')
                    ->select()
                    ->toArray();
                foreach ($data as $ke2 => $va2) {
                    $data[$ke2]['ranking'] = $ke2+1;
                    $data[$ke2]['title'] = Db::name('live')->where('id', '=', $va2['live_id'])->value('title');
                }
            }
        }


        $datas = array(
            'code'=> 0,
            'msg'=> '',
            'count' => 0,
            'data'=> $data
        );
        return json($datas);
    }

    /**
     * 获取最近七天所有日期
     */
    protected function get_weeks($time = '', $format='Y-m-d'){
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        for ($i=1; $i<=7; $i++){
            array_push($date, date($format ,strtotime( '+' . $i-7 .' days', $time)));
        }
        return $date;
    }

    /**
     * 获取最近30天所有日期
     */
    protected function get_month($time = '', $format='Y-m-d'){
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        for ($i=1; $i<=30; $i++){
            array_push($date, date($format ,strtotime( '+' . $i-30 .' days', $time)));
        }
        return $date;
    }


    public function getEchartData()
    {
        $date = $this->request->post('date');

        if (isset($date)) {
            if ($date == 1) {
                $date = $this->get_weeks(time(),'n/j');
            } else if($date == 2) {
                $date = $this->get_month(time(),'n/j');
            }
        } else {
            $date = $this->get_weeks(time(),'n/j');
        }

        $data = [];
        foreach ($date as &$v) {
            $time = date('Ymd', strtotime($v));

            $pv = Db::name('views')->where('date', '=', $time)->count();

            $uv = Db::name('views')->where('date', '=', $time)->group('userid')->count();

            $start = strtotime($v);
            $end = strtotime($v)+86399;
            $learnnow = Db::name('classes_log')
                ->where('resource_id', '=', 0)
                ->where('qiandao', '=', 1)
                ->whereBetween('add_time', [$start, $end])
                ->column('userid');

            $learning = Db::name('learning_user')
                ->where('mode', '=', 2)
                ->whereBetween('add_time', [$start, $end])
                ->column('userid');

            $sign = Db::name('integral_check_in')->where('add_at', '=', date('Y-m-d',strtotime($v)))->column('uid');
            $sign_member = Db::name('members')->where('id', 'in', $sign)->column('userid');
            $hy = count(array_unique(array_merge($learnnow, $learning, $sign_member)));

            //学习人数
            $xx = Db::name('click_log')->where('date', '=', $time)->count();

            //+新增学习人数
            $end = date("Ymd",strtotime("-1 days",strtotime($v)));
            $start = date("Ymd",strtotime("-1 months",strtotime("-1 days",strtotime($v))));
            $mon = Db::name('click_log')->where('date', 'between', [$start, $end])->column('userid');

            $user = Db::name('click_log')->where('date', '=', $time)->column('userid');
            $xz = count(array_diff($user, $mon));


            $arr = [
                'name' => $v,
                'value1' => $pv,
                'value2' => $uv,
                'value3' => $hy,
                'value4' => $xx,
                'value5' => $xz,
            ];

            array_push($data, $arr);
        }

        return $this->outputAjax('获取成功', 0, $data);
    }

    public function getEchartData1()
    {
        $date = $this->request->post('date');

        if (isset($date)) {
            if ($date != '') {
                $t = explode(' - ', $date);
                $date = get_date($t[0],$t[1]);
            } else {
                $date = $this->get_weeks(time(),'n/j');
            }

        } else {
            $date = $this->get_weeks(time(),'n/j');
        }

        $data = [];
        foreach ($date as &$v) {
            $time = date('Ymd', strtotime($v));

            $pv = Db::name('views')->where('date', '=', $time)->count();

            $uv = Db::name('views')->where('date', '=', $time)->group('userid')->count();

            $start = strtotime($v);
            $end = strtotime($v)+86399;
            $learnnow = Db::name('classes_log')
                ->where('resource_id', '=', 0)
                ->where('qiandao', '=', 1)
                ->whereBetween('add_time', [$start, $end])
                ->column('userid');

            $learning = Db::name('learning_user')
                ->where('mode', '=', 2)
                ->whereBetween('add_time', [$start, $end])
                ->column('userid');


            $sign = Db::name('integral_check_in')->where('add_at', '=', date('Y-m-d', strtotime($v)))->column('uid');
            $sign_member = Db::name('members')->where('id', 'in', $sign)->column('userid');
            $hy = count(array_unique(array_merge($learnnow, $learning, $sign_member)));

            //学习人数

            $xx = Db::name('click_log')->where('date', '=', $time)->count();

            //+新增学习人数
            $end = date("Ymd",strtotime("-1 days",strtotime($v)));
            $start = date("Ymd",strtotime("-1 months",strtotime("-1 days",strtotime($v))));
            $mon = Db::name('click_log')->where('date', 'between', [$start, $end])->column('userid');

            $user = Db::name('click_log')->where('date', '=', $time)->column('userid');
            $xz = count(array_diff($user, $mon));


            $arr = [
                'name' => $v,
                'value1' => $pv,
                'value2' => $uv,
                'value3' => $hy,
                'value4' => $xx,
                'value5' => $xz,
            ];

            array_push($data, $arr);
        }

        return $this->outputAjax('获取成功', 0, $data);
    }
}